import random
import uuid

class ProductOperations:
    def __init__(self):
        self._operationsList = [
            self._createProduct,
            self._enumerateProducts,
            self._enumerateFeatures,
            self._modifyProduct,
            self._removeProduct
        ]
        self._baseProduct = {
            'id': 0,
            'name': uuid.uuid4(),
            'partnerId': 0,
            'features': {
                'pair': [
                    {
                        'first': 'AdvancedScheduling',
                        'second': '1'
                    },
                    {
                        'first': 'AllowFilters',
                        'second': '1'
                    }],
            }
        }

    def _getRandomOperation(self):
        return random.choice(self._operationsList)

    def _createProduct(self, manserv, partnerId):
        self._baseProduct['partnerId'] = partnerId
        manserv.AddProduct(**self._baseProduct)

    def _enumerateProducts(self, manserv, partnerId):
        manserv.EnumerateProducts(partnerId = partnerId, currentPartnerOnly = 'true')

    def _enumerateFeatures(self, manserv, partnerId):
        manserv.EnumerateFeatures()

    def _modifyProduct(self, manserv, partnerId):
        result = manserv.EnumerateProducts(partnerId = partnerId, currentPartnerOnly = 'true')
        if len(result.products.productInfo) > 1:
            resultLenth = random.randint(1, len(result.products.productInfo)-1)
            id = result.products.productInfo[resultLenth].id
            if result.products.productInfo[resultLenth].partnerId == partnerId:
                self._baseProduct['id'] = id
                self._baseProduct['name'] = uuid.uuid4()
                self._baseProduct['partnerId'] = partnerId
                manserv.ModifyProduct(**self._baseProduct)

    def _removeProduct(self, manserv, partnerId):
        result = manserv.EnumerateProducts(partnerId = partnerId, currentPartnerOnly = 'true')
        if len(result.products.productInfo) > 10:
            resultLenth = random.randint(1, len(result.products.productInfo)-1)
            id = result.products.productInfo[resultLenth].id
            if result.products.productInfo[resultLenth].partnerId == partnerId:
                manserv.RemoveProduct(productId = id)
